package com.example.demo.Controller;


import com.example.demo.Entity.OperationLog;
import com.example.demo.Entity.User;
import com.example.demo.Service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController//@RestController：统一了我们的返回数据的基本形式为JSON
@RequestMapping("/api/operation-logs")
@CrossOrigin(origins = "*")//允许所有同源访问，防止出现跨域问题
public class OperationLogController {

    private final OperationLogService operationLogService;

    @Autowired
    public OperationLogController(OperationLogService operationLogService) {
        this.operationLogService = operationLogService;
    }

    @PostMapping
    public OperationLog createOperationLog(@RequestBody OperationLog operationLog) {
        return operationLogService.createOperationLog(operationLog);
    }

    @GetMapping("/{id}")
    public Optional<OperationLog> getOperationLogById(@PathVariable Long id) {
        return operationLogService.getOperationLogById(id);
    }

    @PostMapping("/searchLogByUserName")
    public Map<String,Object> searchLogByUserName(@RequestBody User user) {
        System.out.println(operationLogService.searchLogByUserName(user));
        return operationLogService.searchLogByUserName(user);

    }

    @PostMapping("/allLogs")
    public Map<String,Object> getAllOperationLogs() {
        return operationLogService.getAllOperationLogs();
    }

    @PutMapping("/{id}")
    public OperationLog updateOperationLog(@PathVariable Long id, @RequestBody OperationLog operationLog) {
        return operationLogService.updateOperationLog(id, operationLog);
    }

    @DeleteMapping("/{id}")
    public boolean deleteOperationLog(@PathVariable Long id) {
        return operationLogService.deleteOperationLog(id);
    }
}

